package com.dk.service;

import cn.hutool.core.util.StrUtil;
import com.dk.mapper.ReservoirMapper;
import com.dk.page.PageResult;
import com.dk.pojo.Reservoir;
import com.dk.pojo.ReservoirExample;
import com.dk.service.ReservoirService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReservoirServiceimpl implements ReservoirService {

    @Autowired
    private ReservoirMapper reservoirMapper;

    @Override
    public void insertreservoir(Reservoir reservoir) {
        reservoirMapper.insert(reservoir);
    }

    @Override
    public void deleteById(Integer id) {
        int i = reservoirMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAllById(List<String> ids) {
        reservoirMapper.deleteAll(ids);
    }

    @Override
    public PageResult<Reservoir> getAllreservoir(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Reservoir> reservoirs = reservoirMapper.selectByExample(null);
        PageInfo<Reservoir> pageInfo = new PageInfo<>(reservoirs);
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Reservoir> getreservoirByName(String name) {
        ReservoirExample example = new ReservoirExample();
        if (!StrUtil.isEmpty(name) && !"".equals(name)) {
            example.createCriteria().andRNameLike("%" + name + "%");
        }
        List<Reservoir> reservoirs = reservoirMapper.selectByExample(example);
        return reservoirs;
    }

    @Override
    public Reservoir getById(Integer id) {
        return reservoirMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updatereservoir(Reservoir reservoir) {
        reservoirMapper.updateByPrimaryKeySelective(reservoir);
    }
}
